package org.styx;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.BodyHandler;

import java.util.function.Consumer;

public class VertxTest extends AbstractVerticle {
    public static void main(String[] args) {
        String verticleID=VertxTest.class.getName();
        runExample(verticleID);
    }

    @Override
    public void start() throws Exception {
        final Router router=Router.router(vertx);
        router.route().handler(BodyHandler.create());
        router.get("/hello").handler(new Handler<RoutingContext>() {
            @Override
            public void handle(RoutingContext routingContext) {
                routingContext.response().putHeader("content-type","text/html").end("hello world");
            }
        });
        vertx.createHttpServer().requestHandler(new Handler<HttpServerRequest>() {
            @Override
            public void handle(HttpServerRequest httpServerRequest) {
                router.handle(httpServerRequest);
            }
        }).listen(8080);
        System.out.println("启动成功");
    }

    public static void runExample(String verticleId){
        VertxOptions options=new VertxOptions();

        Consumer<Vertx> runner=vertx->{
            vertx.deployVerticle(verticleId);
        };
        Vertx vertx=Vertx.vertx(options);
        runner.accept(vertx);
    }
}
